package com.ewentsai;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.*;
import java.util.ArrayList;
import java.util.Date;

@WebServlet(name = "ReplyDeal")
public class ReplyDeal extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String loginUserName = request.getParameter("loginUserName");
        if(loginUserName.equals("登录")){
            response.sendRedirect("login.jsp");
            return;
        }
        if(request.getParameter("title")==""){
            response.sendRedirect("EmptyError.html");
        }

        ArrayList<Article> articles = new ArrayList<>();
        Connection connection = DB.getConnection();
        Statement statement = DB.createStatement(connection);
        ResultSet rSet = DB.executeQuery(statement, "select * from article");
        try {
            while (rSet.next()) {
                Article article = new Article();
                article.getInitFormRS(rSet);
                articles.add(article);
            }
        }catch (SQLException e){
            e.printStackTrace();
        }
        int id = Integer.parseInt(request.getParameter("size"));
        int pid = Integer.parseInt(request.getParameter("pid"));
        String title = request.getParameter("title");
        String content = request.getParameter("content");
        int ret = DB.executeUpdate(statement,"insert into article values("+id+","+pid+",0,\'"+title+"\',\'"+content+"\',\'"+loginUserName+"\',now(),1,3)");
        if(ret==1){
            response.sendRedirect("submitSuccess.html");
        }
    }
}
